package by.uvaga.wind;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class PaintView extends View {
	
	public static final float MIN_STROKEWIDTH = 1;
	
	public static final float MAX_STROKEWIDTH = 50;
	
	public static float strokeWidth = MIN_STROKEWIDTH;
	
	public static int color = Color.WHITE;

	private  static ArrayList<Point> points = new ArrayList<Point>();
	
	private Bitmap returnedBitmap;
	
	private Point startPoint = new Point();

	public PaintView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}
	
	protected void onDraw(Canvas canvas) {
		if (returnedBitmap != null)
			canvas.drawBitmap(returnedBitmap, 0, 0, null);
		else
			returnedBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(),Bitmap.Config.ARGB_8888);
		
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(this.strokeWidth);
		paint.setColor(this.color);
		Path myPath = new Path();
		Canvas canvas = new Canvas(returnedBitmap);
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			canvas.drawPoint(event.getX(), event.getY(), paint);
			startPoint.x = event.getX();
			startPoint.y = event.getY();
			savePoint(startPoint.x, startPoint.y, true);
		}	
		else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			myPath.moveTo(startPoint.x, startPoint.y);
			for(int i = 0; i< event.getHistorySize() ; i++)
			{
				myPath.lineTo(event.getHistoricalX(i), event.getHistoricalY(i));
				startPoint.x = event.getHistoricalX(i);
				startPoint.y = event.getHistoricalY(i);
				canvas.drawPoint(startPoint.x, startPoint.y, paint);
				savePoint(startPoint.x, startPoint.y, false);
			}
			canvas.drawPath(myPath, paint);
		}
		else if (event.getAction() == MotionEvent.ACTION_UP) {
			canvas.drawPoint(event.getX(), event.getY(), paint);
		}
			invalidate();
			return true;
	}
	
	private void savePoint(float x, float y, boolean isFirst) {
		Point point = new Point();
		point.x = x;
		point.y = y;
		point.first = isFirst;
		points.add(point);
	}
	
	public void back() {
		
		for(int index = points.size()-1; index>=0; index--) {
			if (points.get(index).first) {
				points.remove(index);
				break;
			}
			else
				points.remove(index);
		}
		repaint();
		invalidate();
	}
	
	private void repaint() {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setStyle(Paint.Style.STROKE);
		Path myPath = new Path();
		returnedBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(),Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(returnedBitmap);	
		Log.v("size:", ""+points.size());
		for(Point point:points) {
			paint.setStrokeWidth(point.size);
			paint.setColor(point.color);
			if (point.first) {
				myPath.moveTo(point.x, point.y);
				canvas.drawPoint(point.x, point.y, paint);
			} else {
				myPath.lineTo(point.x, point.y);
				canvas.drawPoint(point.x, point.y, paint);
			}
		}
		canvas.drawPath(myPath, paint);
	}

	public void cleanall() {
		// TODO Auto-generated method stub
		points.clear();
		repaint();
		invalidate();
	}
	
}
